package com.xzkydz.sensor.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.widget.Toast;

/**
 * @Description:复用toast
 */
public class ToastUtils {

    private static class SingletonInstance {
        private static final ToastUtils INSTANCE = new ToastUtils();
    }

    public static ToastUtils getInstance() {
        return SingletonInstance.INSTANCE;
    }

    public void init(Context context) {
        this.context = context;
        handler = new Handler(Looper.getMainLooper());
    }

    private Context context;
    private Toast mToast;
    private Handler handler;

    public void showToast(String text) {
        if (context == null) {
            return;
        }
        handler.post(new Runnable() {
            @Override
            public void run() {
                if (mToast == null) {
                    mToast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
                } else {
                    mToast.setText(text);
                    mToast.setDuration(Toast.LENGTH_SHORT);
                }
                mToast.show();
            }
        });
    }
}
